package com.coolgor.coldot.web.auth;

import com.coolgor.coldot.dto.auth.FunctionExecution;
import com.coolgor.coldot.entity.auth.Function;
import com.coolgor.coldot.entity.auth.Role;
import com.coolgor.coldot.enums.auth.FunctionStateEnum;
import com.coolgor.coldot.service.auth.FunctionService;
import com.coolgor.coldot.common.HttpServletRequestUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;


@Controller
@RequestMapping(value="function", method={RequestMethod.GET,RequestMethod.POST})
public class FunctionController {
	@Autowired
	private FunctionService functionService;

	//老法师信息平台的根节点ID为2,先赋值2为根节点的FunctionID。
	private Long RootFunctionID = 2L ;

	@RequestMapping(value = "/getAllFunctionTree", method = RequestMethod.GET)
	@ResponseBody
	private Map<String, Object> getAllFunctionTree(HttpServletRequest request) {
		Map<String, Object> modelMap = new HashMap<String, Object>();
		Function functionCondition = new Function();
		functionCondition.setFunctionID(RootFunctionID);
		try {
			FunctionExecution functionEx = functionService.getFunctionTree(functionCondition);
			if (functionEx.getState()==FunctionStateEnum.SUCCESS.getState()) {
				modelMap.put("function_tree", functionEx.getFunctionTree());
				modelMap.put("success", true);
			}
			functionEx.getFunctionTree().print();
		} catch (Exception e) {
			modelMap.put("success", false);
			modelMap.put("errMsg", e.toString());
		}
		return modelMap;
	}

	@RequestMapping(value = "/getRoleFunctionTree", method = RequestMethod.GET)
	@ResponseBody
	private Map<String, Object> getRoleFunctionTree(HttpServletRequest request) {
		Map<String, Object> modelMap = new HashMap<String, Object>();
		Long roleID = HttpServletRequestUtil.getLong(request,"role_id");
		if ( roleID >0 ) {
			Role role = new Role();
			role.setRoleID(roleID);
			try {
				Function function = new Function();
				//老法师信息平台的根节点ID为2,先赋值2为根节点的FunctionID。
				function.setFunctionID(RootFunctionID);
				FunctionExecution functionEx = functionService.getFunctionTreeByRole(function, role);
				if (functionEx.getState()==FunctionStateEnum.SUCCESS.getState()) {
					modelMap.put("function_tree", functionEx.getFunctionTree());
					modelMap.put("success", true);
				}
			} catch (Exception e) {
				modelMap.put("success", false);
				modelMap.put("errMsg", e.toString());
			}
		} else {
			modelMap.put("success", false);
			modelMap.put("errMsg", "Empty Role ID");
		}
		return modelMap;
	}

	@RequestMapping(value = "/getLoginRoleFunctionTree", method = RequestMethod.GET)
	@ResponseBody
	private Map<String, Object> getLoginRoleFunctionTree(HttpServletRequest request) {
		Map<String, Object> modelMap = new HashMap<String, Object>();
		Role loginRole = (Role) request.getSession().getAttribute("login_role");
		if (null != loginRole) {
			try {
				Function function = new Function();
				//老法师信息平台的根节点ID为2,先赋值2为根节点的FunctionID。
				function.setFunctionID(RootFunctionID);
				FunctionExecution functionEx = functionService.getFunctionTreeByRole(function, loginRole);
				if (functionEx.getState()==FunctionStateEnum.SUCCESS.getState()) {
					modelMap.put("function_tree", functionEx.getFunctionTree());
					modelMap.put("success", true);
				}
			} catch (Exception e) {
				modelMap.put("success", false);
				modelMap.put("errMsg", e.toString());
			}
		} else {
			modelMap.put("success", false);
			modelMap.put("errMsg", "empty function tree");
		}
		return modelMap;
	}

	@RequestMapping(value = "/getFunction", method = RequestMethod.GET)
	@ResponseBody
	private Map<String, Object> getFunction(HttpServletRequest request) {
		Map<String, Object> modelMap = new HashMap<String, Object>();
		Long functionID = HttpServletRequestUtil.getLong(request,"function_id");
		if ( functionID > 0 ) {

		} else {
			modelMap.put("success", false);
			modelMap.put("errMsg", "Empty Role ID");
		}
		return modelMap;
	}

	@RequestMapping(value = "/addFunction", method = RequestMethod.GET)
	@ResponseBody
	private Map<String, Object> addFunction(HttpServletRequest request) {
		Map<String, Object> modelMap = new HashMap<String, Object>();
		return modelMap;
	}

	@RequestMapping(value = "/modifyFunction", method = RequestMethod.GET)
	@ResponseBody
	private Map<String, Object> modifyFunction(HttpServletRequest request) {
		Map<String, Object> modelMap = new HashMap<String, Object>();
		return modelMap;
	}

	@RequestMapping(value = "/removeFunction", method = RequestMethod.GET)
	@ResponseBody
	private Map<String, Object> removeFunction(HttpServletRequest request) {
		Map<String, Object> modelMap = new HashMap<String, Object>();
		return modelMap;
	}

}
